﻿using BlazorApp3.Client.Application.Services;

namespace BlazorApp3.Client.Application.Features.Counter.Commands.IncreaseCounter;

internal class IncreaseCounterCommandHandler : IRequestHandler<IncreaseCounterCommand, int>
{
    private readonly ICounterRepository _counterRepository;

    private int _step = 2;

    public IncreaseCounterCommandHandler(ICounterRepository counterRepo)
    {
        _counterRepository = counterRepo;
    }

    public async Task<int> Handle(IncreaseCounterCommand request, CancellationToken cancellationToken)
    {
        var currentCount = await _counterRepository.GetCurrentCounter();
        currentCount += _step;
        await _counterRepository.SaveCounter(currentCount);
        return currentCount;
    }
}